package leetcode.leetcode20;

import java.util.Stack;

/**
 * @author czt
 * @version 1.0
 * @since 2024/12/20
 */
public class Leetcode20 {

    public static void main(String[] args) {
        boolean valid = isValid("]");
        System.out.println("valid = " + valid);
    }

    public static boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        if (s == null || s.length() == 0) {
            return true;
        }

        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == '(') {
                stack.push(')');
            }else if (c == '[') {
                stack.push(']');
            } else if (c == '{') {
                stack.push('}');
            } else {
                if (!stack.isEmpty() && c == stack.peek()) {
                    stack.pop();
                } else {
                    return false;
                }
            }
        }
        return stack.isEmpty();
    }
}
